<template>
  <div id="app">
    <!-- 顶部导航栏 -->
    <nav v-if="isAuthenticated" class="top-nav">
      <div class="nav-container">
        <div class="nav-brand">
          <h3>🍎 Fruit Management System</h3>
        </div>
        <div class="nav-links">
          <router-link to="/">Home</router-link>
          <router-link to="/category">Category</router-link>
          <router-link to="/fruit">Fruit</router-link>
          <router-link to="/warehouse">Warehouse</router-link>
        </div>
        <div class="nav-user">
          <el-dropdown>
            <span class="el-dropdown-link">
              <el-icon><UserFilled /></el-icon>
              {{ currentUser?.username || 'User' }}
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item>Profile</el-dropdown-item>
                <el-dropdown-item divided @click="handleLogout">Logout</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </div>
    </nav>

    <!-- 主要内容区域 -->
    <div :class="{ 'main-content': isAuthenticated }">
      <router-view />
    </div>
  </div>
</template>

<script setup>
import { ref, watch } from 'vue';
import { useRouter, useRoute } from 'vue-router';
import { ElMessage, ElMessageBox } from 'element-plus';
import { UserFilled } from '@element-plus/icons-vue';
import { isAuthenticated as checkAuth, getUser, clearAuth } from '@/utils/auth';

const router = useRouter();
const route = useRoute();

const isAuthenticated = ref(checkAuth());
const currentUser = ref(getUser());

// 监听路由变化，更新认证状态
watch(() => route.path, () => {
  isAuthenticated.value = checkAuth();
  currentUser.value = getUser();
});

// 退出登录
const handleLogout = () => {
  ElMessageBox.confirm('Are you sure you want to logout?', 'Confirm', {
    confirmButtonText: 'Confirm',
    cancelButtonText: 'Cancel',
    type: 'warning'
  }).then(() => {
    clearAuth();
    ElMessage.success('Logged out successfully');
    router.push('/login');
  }).catch(() => {
    // 取消退出
  });
};
</script>

<style>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

#app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  min-height: 100vh;
}

/* 顶部导航栏 */
.top-nav {
  background: #fff;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  position: sticky;
  top: 0;
  z-index: 1000;
}

.nav-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
  height: 60px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.nav-brand h3 {
  margin: 0;
  font-size: 20px;
  color: #333;
  font-weight: 600;
}

.nav-links {
  display: flex;
  gap: 30px;
  flex: 1;
  justify-content: center;
}

.nav-links a {
  text-decoration: none;
  color: #666;
  font-size: 16px;
  font-weight: 500;
  transition: color 0.3s;
  padding: 5px 10px;
}

.nav-links a:hover {
  color: #409eff;
}

.nav-links a.router-link-exact-active {
  color: #409eff;
  border-bottom: 2px solid #409eff;
}

.nav-user {
  cursor: pointer;
}

.el-dropdown-link {
  display: flex;
  align-items: center;
  gap: 5px;
  color: #333;
  font-size: 14px;
  cursor: pointer;
}

.el-dropdown-link:hover {
  color: #409eff;
}

/* 主要内容区域 */
.main-content {
  padding: 20px;
  max-width: 1200px;
  margin: 0 auto;
}
</style>
